package com.example.shop.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.shop.pojo.Department;
import com.example.shop.pojo.DepartmentDto;
import com.example.shop.pojo.Logs;
import com.example.shop.pojo.UserAuth;
import com.example.shop.pojo.UserDepartment;
import com.example.shop.pojo.UserDto;
import com.example.shop.service.DepartmentService;
import com.example.shop.service.UserAuthService;
import com.example.shop.utils.AutoLogout;
import com.example.shop.utils.LogsLevel;
import com.example.shop.utils.LogsRecord;
import com.example.shop.utils.Result;
import com.example.shop.utils.UserIdSession;

@RestController
@RequestMapping("/department")
public class DepartmentController {
	// 日志对象
	private final static Logger logger = LoggerFactory.getLogger(DepartmentController.class);

	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private LogsRecord logsRecord;
	@Autowired
	private UserAuthService userAuthService;

	/**
	 * 添加部门
	 */
	@PostMapping("/add")
	public Result<String> add(@RequestBody DepartmentDto departmentDto) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(departmentDto.getUserAccount());
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(departmentDto.getUserAccount());

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(departmentDto.getUserAccount(), 18L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = departmentDto.getUserAccount() + "添加部门：" + departmentDto.getDepartmentName();
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		// 添加部门
		String result = departmentService.add(departmentDto);
		if (!"部门添加成功".equals(result)) {
			return Result.fail(result);
		}
		return Result.success(result);
	}

	/**
	 * 删除部门
	 */
	@DeleteMapping("/remove")
	public Result<String> delete(@RequestBody DepartmentDto department) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(department.getUserAccount());
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(department.getUserAccount());

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(department.getUserAccount(), 19L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = department.getUserAccount() + "删除部门" + " id:" + department.getDepartmentId() + "名称："
				+ department.getDepartmentName();
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		String result = departmentService.delete(department);
		if (!"部门删除成功".equals(result)) {
			return Result.fail(result);
		}
		return Result.success(result);
	}

	/**
	 * 查询部门:查询同级，同上司的部门基本信息
	 */
	@GetMapping("/select/info")
	public Result<DepartmentDto> selectBaseInfo(@RequestBody DepartmentDto department) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(department.getUserAccount());
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(department.getUserAccount());

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(department.getUserAccount(), 16L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 查询部门
		List<DepartmentDto> result = departmentService.selectBaseInfo(department);
		if (result == null) {
			return Result.fail("");
		}

		// 记录日志
		String msg = department.getUserAccount() + "查询同级，同上司的部门基本信息：" + result;
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		return Result.success(result);
	}

	/**
	 * 查询子部门基本信息
	 */
	@GetMapping("/select/son/department")
	public Result<Department> selectSonDepartInfo(@RequestBody DepartmentDto department) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(department.getUserAccount());
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(department.getUserAccount());

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(department.getUserAccount(), 16L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = department.getUserAccount() + "查询子部门基本信息";
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		// 查询部门
		List<Department> sonDepartments = departmentService.selectSDP(department);
		if (sonDepartments.isEmpty()) {
			return Result.fail(null);
		}
		return Result.success(sonDepartments);
	}

	/**
	 * 查询子部门人员基本信息
	 */
	@GetMapping("/select/son/people")
	public Result<DepartmentDto> selectSonPeopleInfo(@RequestBody DepartmentDto department) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(department.getUserAccount());
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(department.getUserAccount());

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(department.getUserAccount(), 16L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = department.getUserAccount() + "查询子部门人员基本信息";
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		// 查询部门
		List<UserDto> sonDepartmentPeoples = departmentService.selectSSPI(department);
		if (sonDepartmentPeoples == null || sonDepartmentPeoples.size() == 0) {
			return Result.fail("查询失败");
		}
		return Result.success(sonDepartmentPeoples);
	}

	/**
	 * 部门人员的更新
	 */
	@PutMapping("/update/people/{account}")
	public Result<String> updateUserDepartment(@PathVariable("account") String account,@RequestBody UserDepartment userDepartment) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(account);
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(account);

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(account, 17L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = account +"对"+userDepartment.getUserAccount() + "进行部门人员的更新：" + userDepartment.toString();
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		// 部门人员的更新
		String result = departmentService.updateUserDepartment(userDepartment);
		if (!"部员更新成功".equals(result)) {
			return Result.fail(result);
		}
		return Result.success(result);
	}

	/**
	 * 部门人员查询
	 */
	@GetMapping("/select/people/{account}")
	public Result<UserDepartment> selectUserDepartment(@PathVariable("account") String account,@RequestBody UserDepartment userDepartment) {

		// 登录校验
		Object employee = UserIdSession.getAttribute(account);
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(account);

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(account, 16L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg =account+"对"+ userDepartment.getUserAccount() + "进行部门人员的查询";
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		// 部门人员的查询
		UserDepartment ud = departmentService.selectUserDepartment(userDepartment);
		if (ud == null) {
			return Result.fail("查询失败");
		}
		return Result.success(ud);
	}

	/**
	 * 部门职位查询
	 */
	@GetMapping("/select/position/{account}")
	public Result<UserDepartment> selectUserPosition(@PathVariable("account") String account,@RequestBody UserDepartment userDepartment) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(account);
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(account);

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(account, 16L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = account +"对"+userDepartment.getUserAccount()+ "部门职位查询";
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		UserDepartment ud = departmentService.selectUserDepartment(userDepartment);
		if (ud == null) {
			return Result.fail("查询失败");
		}
		return Result.success(ud);
	}

	/**
	 * 部门职位更新
	 */
	@PutMapping("/update/position/{account}")
	public Result<String> updateUserPosition(@PathVariable("account") String account,@RequestBody UserDepartment userDepartment) {
		// 登录校验
		Object employee = UserIdSession.getAttribute(account);
		if (employee == null) {
			return Result.fail("NOTLOGIN");
		}

		// 记录用户操作
		AutoLogout.userPerformedOperation(account);

		// 权限校验
		List<UserAuth> userAuthLists = new ArrayList<UserAuth>();
		userAuthLists.add(new UserAuth(account, 17L, 0L));
		String userAuthResult = userAuthService.checkAuth(userAuthLists);
		if (!"权限验证通过".equals(userAuthResult)) {
			return Result.fail(userAuthResult);
		}
		;

		// 记录日志
		String msg = account +"对"+userDepartment.getUserAccount()+ "进行部门职位更新";
		logger.debug(msg);
		logsRecord.addLogs(new Logs(null, LocalDateTime.now().toString(), LogsLevel.LOG_LEVEL_SELECT, msg));

		String result = departmentService.updateUserDepartment(userDepartment);
		if (!"部员更新成功".equals(result)) {
			return Result.fail(result);
		}
		return Result.success(result);
	}
	
} 
